package com.gw.vip.server.service.dao_service;

import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.server.dto.QueryDto;
import com.gw.vip.server.po.BsOrder;
import com.gw.vip.server.po.HisOrder;
import com.gw.vip.server.po.Trans;
import com.gw.vip.server.vo.MainAccResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-06-25
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class OrderAllService {

    private final BsOrderService bsOrderService;
    private final HisOrderService hisOrderService;
    private final TransAllService transAllService;

    /**
     * 根据shopId和客户订单号查询订单。会联动查询历史表
     * @param shopId
     * @param clientOrder
     * @return
     */
    public BsOrder getByBase(String shopId, String clientOrder){
        BsOrder bsOrder = bsOrderService.getByBase(shopId, clientOrder);
        if (bsOrder !=null){
            return bsOrder;
        }

        //从历史表查询
        HisOrder hisOrder = hisOrderService.getByBase(shopId, clientOrder);
        if (hisOrder ==null){
            return null;
        }
        bsOrder = new BsOrder();
        BeanUtils.copyProperties(hisOrder, bsOrder);
        return bsOrder;
    }

    /**
     * 更新订单，先更新trans订单，如果失败，尝试更新hisTrans订单。
     * @param bsOrder
     */
    public boolean updateById(BsOrder bsOrder) {
        if (bsOrder==null){
            log.error("bsOrder为空！");
            throw new ServiceException("bsOrder为空！");
        }

        if (StringUtils.isBlank(bsOrder.getId())){
            log.error("id为空！");
            throw new ServiceException("id为空！");
        }

        if (bsOrderService.updateById(bsOrder)){
            return true;
        }

        HisOrder hisOrder = new HisOrder();
        BeanUtils.copyProperties(bsOrder, hisOrder);
        return hisOrderService.updateById(hisOrder);
    }

    /**
     * 根据id获取订单信息
     * @param orderId
     * @return
     */
    public BsOrder getById(String orderId) {
        if (StringUtils.isBlank(orderId)){
            log.error("orderId为空！");
        }

        BsOrder bsOrder = bsOrderService.getById(orderId);
        if (bsOrder ==null){
            //从历史表查找
            HisOrder hisOrder = hisOrderService.getById(orderId);
            if (hisOrder ==null){
                return null;
            }
            bsOrder = new BsOrder();
            BeanUtils.copyProperties(hisOrder, bsOrder);
        }
        return bsOrder;
    }

    /**
     * 订单查询
     * @param dto
     * @return
     */
    public MainAccResponse orderQuery(QueryDto dto) {
        BsOrder bsOrder = this.getByBase(dto.getShopId(), dto.getClientOrder());
        if (bsOrder ==null){
            log.error("订单不存在。shopId【{}】,客户单号【{}】", dto.getShopId(), dto.getClientOrder());
            throw new ServiceException(ExceptionEnums.ITEM_NOT_EXIST, "订单不存在");
        }

        //原交易明细
        List<Trans> transList = transAllService.getByOrderId(bsOrder.getId());

        MainAccResponse mainAccResponse = new MainAccResponse();
        mainAccResponse.setOrderId(bsOrder.getId());
        mainAccResponse.setBalAmt(bsOrder.getBalAmt());
        mainAccResponse.setStatus(bsOrder.getStatus());
        mainAccResponse.setTransList(transList);
        return mainAccResponse;
    }
}
